package com.zking.web.lsc;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zking.dao.domain.CollectionPosition;
import com.zking.dao.domain.Information;
import com.zking.dao.domain.Post;
import com.zking.service.lsc.SXservice;

@Controller
public class CXController {
	@Autowired
	private SXservice sxservice;

	@RequestMapping(value = "/gg")
	public String sj(Integer informationid,Integer userid, Integer id, CollectionPosition collectionPosition,
			HttpServletRequest request) {
		System.out.println("userid"+userid+"id"+id+"informationid"+informationid);
		CollectionPosition cp= sxservice.getCollectionPosition(userid, id);
		if (cp==null) {
			Post post = sxservice.getpostone(id);
			Information l2 = sxservice.getInformationone(informationid);
			collectionPosition.setJob_picture(l2.getCom_logo());// 图片
			collectionPosition.setJob_webite(l2.getCom_website());// 网址
			collectionPosition.setJob_name(post.getJob_name());
			collectionPosition.setJob_salary(post.getJob_salary());
			collectionPosition.setJob_time(post.getJob_time());
			collectionPosition.setJob_city(post.getJob_city());
			collectionPosition.setJob_experience(post.getJob_experience());
			collectionPosition.setJob_academic(post.getJob_academic());
			collectionPosition.setJob_welfare(post.getJob_welfare());
			collectionPosition.setUserid(userid);
			collectionPosition.setInformationid(informationid);;
			collectionPosition.setPostid(id);
			sxservice.zj(collectionPosition);
			request.getSession().setAttribute("tis", "收藏成功");
			return "redirect:/com?id=" + id;
		}else {
			int i = sxservice.deletecp(cp.getId());
			request.getSession().setAttribute("tis", "取消收藏成功");
			return "redirect:/com?id=" + id;
		}
		
	}
//	CollectionPosition l4 = sxservice.gf(userid);
//	if (l4 == null) {
//		List<Post> list = sxservice.sj();
//		if (list != null) {
//			Post l2 = sxservice.idxs(a);
//			if (l2 != null) {
//				collectionPosition.setJob_picture(l2.getJob_picture());// 图片
//				collectionPosition.setJob_webite(l2.getJob_webite());// 网址
//				collectionPosition.setJob_name(l2.getJob_name());
//				collectionPosition.setJob_salary(l2.getJob_salary());
//				collectionPosition.setJob_time(l2.getJob_time());
//				collectionPosition.setJob_city(l2.getJob_city());
//				collectionPosition.setJob_experience(l2.getJob_experience());
//				collectionPosition.setJob_academic(l2.getJob_academic());
//				collectionPosition.setJob_welfare(l2.getJob_welfare());
//				collectionPosition.setUserid(1);
//				int i = sxservice.zj(collectionPosition);
//				String tis = "收藏成功";
//				request.getSession().setAttribute("tis", tis);
//				
//				return "redirect:/com?id=" + id;
//			} else {
//				return "redirect:/com?id=" + id;
//			}
//		} else {
//			return "redirect:/com?id=" + id;
//		}
//	} else {
//		int l5 = sxservice.gl(userid);
//		if (l5 == 0) {
//			String tis = "没有取消收藏";
//			request.getSession().setAttribute("tis", tis);
//			return "lk?userid=1" + id;
//		} else {
//			String tis = "以取消收藏";
//			request.getSession().setAttribute("tis", tis);
//			return "redirect:/com?id=" + id;
//		}
//
//	}
	@RequestMapping(value = "/lk")
	public String zp1(Integer userid, HttpServletRequest request) {
		List<CollectionPosition> ps = sxservice.zp1(userid);
		if (!ps.isEmpty()) {
			request.setAttribute("collectionPosition", ps);
			return "collections";
		}else {
			request.setAttribute("tisshi", "你还没有收藏职位!");
			return "collections";
		}
		
	}

	@RequestMapping(value = "/kn")
	public ModelAndView nl(Integer id, Integer userid, HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("redirect:/lk?userid=" + userid);
		int ps1 = sxservice.nl(id);
		if (ps1 != 0) {
			modelAndView.addObject("collectionPosition", userid);
			return modelAndView;
		}
		return modelAndView;
	}

}
